// Test suite for the time.js module
(function() {
     module('lily.time');

     var clock,
	 rt = 0,
	 tick = function(lag) {
	     ok(this.fps <= 50, 'fps should be less or equal to 50. Actual: '
		                + this.fps);
	     ok(lag >= 1, 'lag should be greater or equal to 1. Actual: '
		          + lag);
	     rt++;
	     if (rt == 100) {
		 this.stop();
		 setTimeout(function() {
		     equal(rt, 100, 'rt should be equal to 100');
		 }, 1000);
	     }
	 };


     test('constructor', function() {
	 clock = new lily.time.Clock(tick, 50, false);
	 strictEqual(clock.onTick, tick, 'function reference and event should '
		                         + 'be the same');
	 equal(clock.max_fps, 50, 'max_fps should be equal to 50');
	 equal(clock._interval, 20, 'clock interval should be 1000/50 = 20');
     });

     asyncTest('frame rate', 201, function() {
	 clock.start();
	 setTimeout(start, 5000);
     });

     test('change frame rate', function() {
	  clock.changeFPS(25);
	  equal(clock.max_fps, 25, 'max_fps should be equal to 25');
	  equal(clock._interval, 40, 'clock interval should be 1000/25 = 40');
     });

     asyncTest('assert framerate is kept after changing', function() {
	 var st = +new Date;
	 clock.onTick = function(lag) {
	     ok(this.fps <= 25, 'fps should be less or equal to 25. Actual: '
	                        + this.fps);
	     ok(lag >= 1, 'lag should be greater or equal to 1. Actual: '
		          + lag);
	     if (+new Date - st >= 2000) {
		 this.stop();
	     }
	 };
	 setTimeout(start, 3000);
     });
}());
